package cn.iocoder.yudao.module.ride.controller.admin.training;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ride.controller.admin.training.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.training.TrainingDO;
import cn.iocoder.yudao.module.ride.service.training.TrainingService;

@Tag(name = "管理后台 - 司机培训课程")
@RestController
@RequestMapping("/ride/training")
@Validated
public class TrainingController {

    @Resource
    private TrainingService trainingService;

    @PostMapping("/create")
    @Operation(summary = "创建司机培训课程")
    @PreAuthorize("@ss.hasPermission('ride:training:create')")
    public CommonResult<Long> createTraining(@Valid @RequestBody TrainingSaveReqVO createReqVO) {
        return success(trainingService.createTraining(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新司机培训课程")
    @PreAuthorize("@ss.hasPermission('ride:training:update')")
    public CommonResult<Boolean> updateTraining(@Valid @RequestBody TrainingSaveReqVO updateReqVO) {
        trainingService.updateTraining(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除司机培训课程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ride:training:delete')")
    public CommonResult<Boolean> deleteTraining(@RequestParam("id") Long id) {
        trainingService.deleteTraining(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除司机培训课程")
                @PreAuthorize("@ss.hasPermission('ride:training:delete')")
    public CommonResult<Boolean> deleteTrainingList(@RequestParam("ids") List<Long> ids) {
        trainingService.deleteTrainingListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得司机培训课程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ride:training:query')")
    public CommonResult<TrainingRespVO> getTraining(@RequestParam("id") Long id) {
        TrainingDO training = trainingService.getTraining(id);
        return success(BeanUtils.toBean(training, TrainingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得司机培训课程分页")
    @PreAuthorize("@ss.hasPermission('ride:training:query')")
    public CommonResult<PageResult<TrainingRespVO>> getTrainingPage(@Valid TrainingPageReqVO pageReqVO) {
        PageResult<TrainingDO> pageResult = trainingService.getTrainingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TrainingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出司机培训课程 Excel")
    @PreAuthorize("@ss.hasPermission('ride:training:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTrainingExcel(@Valid TrainingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TrainingDO> list = trainingService.getTrainingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "司机培训课程.xls", "数据", TrainingRespVO.class,
                        BeanUtils.toBean(list, TrainingRespVO.class));
    }

}